// HSS.Forms.DataGridViewCalendarEditingControl.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DataGridViewCalendarEditingControl.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System;
	using System.Windows.Forms;
	#endregion

	#region DataGridViewCalendarEditingControl
	/// <summary>
	/// A Custom DataGridView Editing Control. Allows a DataGridView Colum/Cell to contain a DateTime Value
	/// and provide a Calendard DateTimePicker for selection.
	/// </summary>
	[System.ComponentModel.ToolboxItem(false)]
	public class DataGridViewCalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
	{
		DataGridView dataGridView;
		private bool valueChanged = false;
		int rowIndex;

		/// <summary>
		/// Constructor
		/// </summary>
		public DataGridViewCalendarEditingControl()
		{
			this.Format = DateTimePickerFormat.Short;
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.EditingControlFormattedValue property.
		/// </summary>
		public object EditingControlFormattedValue
		{
			get
			{
				return this.Value.ToShortDateString();
			}
			set
			{
				String newValue = value as String;
				if (newValue != null)
				{
					this.Value = DateTime.Parse(newValue);
				}
			}
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.GetEditingControlFormattedValue method.
		/// </summary>
		/// <param name="context">The current DataGridViewDataErrorContexts</param>
		/// <returns>The Formatted Value</returns>
		public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
		{
			return EditingControlFormattedValue;
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
		/// </summary>
		/// <param name="dataGridViewCellStyle">DataGridViewCellStyle</param>
		public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
		{
			this.Font = dataGridViewCellStyle.Font;
			this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
			this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.EditingControlRowIndex property.
		/// </summary>
		public int EditingControlRowIndex
		{
			get
			{
				return rowIndex;
			}
			set
			{
				rowIndex = value;
			}
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method.
		/// </summary>
		/// <param name="key">The Keys to monitor for Input</param>
		/// <param name="dataGridViewWantsInputKey">Whether or not the DataGride wants input</param>
		/// <returns>true if the Editing Controls want's input otherwise false</returns>
		public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
		{
			// Let the DateTimePicker handle the keys listed.
			switch (key & Keys.KeyCode)
			{
				case Keys.Left:
				case Keys.Up:
				case Keys.Down:
				case Keys.Right:
				case Keys.Home:
				case Keys.End:
				case Keys.PageDown:
				case Keys.PageUp:
					return true;
				default:
					return false;
			}
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit method.
		/// </summary>
		/// <param name="selectAll">Indicates whether or not to Select All Cells</param>
		/// <remarks>
		/// No preparation required, this method does nothing.
		/// </remarks>
		public void PrepareEditingControlForEdit(bool selectAll)
		{
			// No preparation needs to be done.
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange property.
		/// </summary>
		/// <remarks>Always returns false</remarks>
		public bool RepositionEditingControlOnValueChange
		{
			get
			{
				return false;
			}
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.EditingControlDataGridView property.
		/// </summary>
		public DataGridView EditingControlDataGridView
		{
			get
			{
				return dataGridView;
			}
			set
			{
				dataGridView = value;
			}
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.EditingControlValueChanged property.
		/// </summary>
		public bool EditingControlValueChanged
		{
			get
			{
				return valueChanged;
			}
			set
			{
				valueChanged = value;
			}
		}

		/// <summary>
		/// Implements the IDataGridViewEditingControl.EditingPanelCursor property.
		/// </summary>
		/// <remarks>
		/// Will always return the base Cursor
		/// </remarks>
		public virtual Cursor EditingPanelCursor
		{
			get
			{
				return base.Cursor;
			}
		}

		/// <summary>
		/// OnValueChanged
		/// </summary>
		/// <param name="e">EventArgs</param>
		protected override void OnValueChanged(EventArgs e)
		{
			// Notify the DataGridView that the contents of the cell
			// have changed.
			valueChanged = true;
			this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
			base.OnValueChanged(e);
		}
	}
	#endregion
}